package com.passive.lock.service.user.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.passive.lock.mapper.TRegionMapper;
import com.passive.lock.mapper.TRegionMapperCustom;
import com.passive.lock.model.po.TRegion;
import com.passive.lock.model.po.TRegionExample;
import com.passive.lock.model.vo.TRegionVo;
import com.passive.lock.service.user.RegionService;
import com.passive.lock.utils.DozerMapperUtils;
import com.passive.lock.utils.Result;
import com.passive.lock.utils.tree.TreeRegionUtil;

@Service("regionService")
public class RegionServiceImpl implements RegionService {
	@Resource(name = "TRegionMapper")
	private TRegionMapper regionMapper;
	@Resource(name = "TRegionMapperCustom")
	private TRegionMapperCustom regionMapperCustom;

	@Override
	public Result regionTree(Integer regionid) {
		TRegion tRegion = regionMapper.selectByPrimaryKey(regionid);
		String regionidlike = regionid.toString();
		regionidlike = regionidlike.substring(0,2);
		List<TRegion> list = regionMapperCustom.regionTree(regionid,regionidlike);
		List<TRegionVo> listTree = new ArrayList<TRegionVo>();

		for (TRegion tree : list) {
			TRegionVo tRegionVo = new TRegionVo();
			DozerMapperUtils.map(tree, tRegionVo);
			listTree.add(tRegionVo);
		}
		TRegion regiontree = tRegion;
		for (int i = 0; i < Integer.parseInt(tRegion.getLeveltype()); i++) {
			TRegion d = regionMapper.selectByPrimaryKey(regiontree.getParentid());
			TRegionVo tRegionVo = new TRegionVo();
			DozerMapperUtils.map(d, tRegionVo);
			listTree.add(tRegionVo);
			regiontree = d;
		}
		List<TRegionVo> newTreeDataList = TreeRegionUtil.getfatherNode(listTree);
		return Result.ok(newTreeDataList);
	}

	@Override
	public List<TRegionVo> regionList(Integer parentId, String regionName, Integer offset, Integer limit) {
		try {
			List<TRegion> list = regionMapperCustom.regionList(parentId, regionName, offset, limit);
			List<TRegionVo> listvo = new ArrayList<TRegionVo>();
			for (TRegion tRegion : list) {
				String[] stringArr = tRegion.getMergername().split(",");
				TRegionVo tRegionVo = new TRegionVo();
				DozerMapperUtils.map(tRegion, tRegionVo);
				if (stringArr.length > 0) {
					tRegionVo.setChinese(stringArr[0]);
				}
				if (stringArr.length > 1) {
					tRegionVo.setProname(stringArr[1]);
				}
				if (stringArr.length > 2) {
					tRegionVo.setCityname(stringArr[2]);
				}
				if (stringArr.length > 3) {
					tRegionVo.setArename(stringArr[3]);
				}
				if (stringArr.length > 4) {
					tRegionVo.setCountry(stringArr[4]);
				}
				if (stringArr.length > 5) {
					tRegionVo.setVillage(stringArr[5]);
				}
				if (stringArr.length > 6) {
					tRegionVo.setCustom(stringArr[6]);
				}
				listvo.add(tRegionVo);
			}
			return listvo;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	@Override
	public int regionListCount(Integer parentId, String regionName) {
		try {
			int count = regionMapperCustom.regionListCount(parentId, regionName);
			return count;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}

	@Override
	public Result addRegion(TRegionVo tRegionVo) {
		TRegion tRegion = new TRegion();
		String fid = regionMapperCustom.regionId(tRegionVo.getParentid());
		Integer id;
		if (fid == null) {
			id = tRegionVo.getParentid() * 100;
		} else {
			id = Integer.parseInt(fid) + 1;
		}
		TRegion fRegion = regionMapper.selectByPrimaryKey(tRegionVo.getParentid());
		String mergerName = fRegion.getMergername() + "," + tRegionVo.getName();
		DozerMapperUtils.map(tRegionVo, tRegion);
		tRegion.setId(id);
		tRegion.setMergername(mergerName);
		int  leveltype = Integer.parseInt(fRegion.getLeveltype())+1;
		tRegion.setLeveltype( Integer.toString(leveltype));
		regionMapper.insert(tRegion);
		return Result.ok();
	}

	@Override
	public Result updateRegion(TRegionVo tRegionVo) {
		try {
			TRegion tRegion = new TRegion();
			DozerMapperUtils.map(tRegionVo, tRegion);
			regionMapper.updateByPrimaryKey(tRegion);
			return Result.ok();
		} catch (Exception e) {
			return Result.build(500, "请联系系统管理员");
		}
	}

	@Override
	public Result deleteRegionList(List<Integer> regionids) {
		for (Integer regionid : regionids) {
			regionMapper.deleteByPrimaryKey(regionid);
		}
		return Result.ok();
	}

	@Override
	public Result deleteRegion(TRegionVo tRegionVo) {
		int count = regionMapper.deleteByPrimaryKey(tRegionVo.getId());
		return Result.ok(count);
	}

	@Override
	public Result queryRegion(Integer regionid) {
		TRegion tRegion = regionMapper.selectByPrimaryKey(regionid);
		String[] stringArr = tRegion.getMergername().split(",");
		TRegionVo tRegionVo = new TRegionVo();
		DozerMapperUtils.map(tRegion, tRegionVo);
		if (stringArr.length > 0) {
			tRegionVo.setChinese(stringArr[0]);
		}
		if (stringArr.length > 1) {
			tRegionVo.setProname(stringArr[1]);
		}
		if (stringArr.length > 2) {
			tRegionVo.setCityname(stringArr[2]);
		}
		if (stringArr.length > 3) {
			tRegionVo.setArename(stringArr[3]);
		}
		if (stringArr.length > 4) {
			tRegionVo.setCountry(stringArr[4]);
		}
		if (stringArr.length > 5) {
			tRegionVo.setVillage(stringArr[5]);
		}
		if (stringArr.length > 6) {
			tRegionVo.setCustom(stringArr[6]);
		}
		return Result.ok(tRegionVo);
	}

	@Override
	public Result parentRegion(Integer regionid) {
		TRegion tRegion1 = regionMapper.selectByPrimaryKey(regionid);
		TRegion tRegion2 = regionMapper.selectByPrimaryKey(tRegion1.getParentid());
		TRegion tRegion3 = regionMapper.selectByPrimaryKey(tRegion2.getParentid());
		Map<String, TRegion> resultMap = new HashMap<>();
		resultMap.put("are", tRegion1);
		resultMap.put("city", tRegion2);
		resultMap.put("pro", tRegion3);
		return Result.ok(resultMap);
	}

	@Override
	public Result childrenRegion(Integer parentId) {
		TRegionExample example = new TRegionExample();
		TRegionExample.Criteria Criteria = example.createCriteria();
		Criteria.andParentidEqualTo(parentId);
		List<TRegion> List = regionMapper.selectByExample(example);
		return Result.ok(List);
	}

}
